package cn.edu.stu.syllabus.base;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lauzy.freedom.lbehaviorlib.behavior.CommonBehavior;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.edu.stu.syllabus.R;
import cn.edu.stu.syllabus.switcher.ThemeStyleSwitcher;
import cn.edu.stu.syllabus.widgets.DoubleClickListener;

/**
 * Created by daidaijie on 2017/3/4.
 */

public abstract class BaseActivity extends RxAppCompatActivity implements AppBarShowHideListener {

    protected int mDeviceWidth;
    protected int mDeviceHeight;

    protected int mStatusBarHeight;

    @Nullable
    @BindView(R.id.topView)
    protected View mTopView;
    @Nullable
    @BindView(R.id.toolbar)
    protected Toolbar mToolbar;
    @Nullable
    @BindView(R.id.appbar)
    protected AppBarLayout mAppbar;
    @Nullable
    @BindView(R.id.titleTextView)
    protected TextView mTitleTextView;

    OnToolbarDoubleClickListener mOnToolbarDoubleClickListener;

    @Override
    public void onShow() {
        if (mAppbar != null) {
            CommonBehavior.from(mAppbar).show();
        }
    }

    @Override
    public void onHide() {
        if (mAppbar != null) {
            CommonBehavior.from(mAppbar).hide();
        }
    }

    public interface OnToolbarDoubleClickListener {
        void onToolbarDoubleClick();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initTheme();

        setContentView(getContentViewId());
        ButterKnife.bind(this);

        initSize();

        if (mToolbar != null) {
            initToolbar();
        }

        if (mTopView != null) {
            initTopView();
        }

        init(savedInstanceState);
    }

    private void initTopView() {
        mTopView.getLayoutParams().height = getStatusBarHeight();
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            mTopView.setVisibility(View.GONE);
        } else {
            mTopView.setVisibility(View.VISIBLE);
        }
    }

    private void initTheme() {
        setTheme(ThemeStyleSwitcher.getInstance().getCurrentThemeStyle().styleRec);
    }

    private void initSize() {
        mDeviceWidth = getWindowManager().getDefaultDisplay().getWidth();
        mDeviceHeight = getWindowManager().getDefaultDisplay().getHeight();
        mStatusBarHeight = getStatusBarHeight();
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            mDeviceHeight -= mStatusBarHeight;
        }
    }

    @LayoutRes
    protected abstract int getContentViewId();

    protected void initToolbar() {
        //透明状态栏并且适应Toolbar的高度
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            ViewGroup.LayoutParams layoutParams = mToolbar.getLayoutParams();
            layoutParams.height = layoutParams.height + mStatusBarHeight;
        }
//        setSupportActionBar(mToolbar);
//        mToolbar.setDisplayShowTitleEnabled(false);
//        mToolbar.setDisplayHomeAsUpEnabled(true);
        mToolbar.setNavigationIcon(ContextCompat.getDrawable(this, R.drawable.ic_arrow_back_white_24dp));
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseActivity.this.onBackPressed();
            }
        });
        mToolbar.setOnClickListener(new DoubleClickListener() {
            @Override
            public void onDoubleClick() {
                if (mOnToolbarDoubleClickListener != null) {
                    mOnToolbarDoubleClickListener.onToolbarDoubleClick();
                }
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    protected abstract void init(Bundle savedInstanceState);

    protected void setTitle(String title) {
        if (mTitleTextView != null) {
            mTitleTextView.setText(title);
        }
    }

    public OnToolbarDoubleClickListener getOnToolbarDoubleClickListener() {
        return mOnToolbarDoubleClickListener;
    }

    public void setOnToolbarDoubleClickListener(OnToolbarDoubleClickListener onToolbarDoubleClickListener) {
        mOnToolbarDoubleClickListener = onToolbarDoubleClickListener;
    }

    public int getDeviceWidth() {
        return mDeviceWidth;
    }

    public int getDeviceHeight() {
        return mDeviceHeight;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Glide.get(this).clearMemory();
    }
}
